﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;

namespace RESTTest
{
    [TestClass]
    public class ConsultaTEST
    {
        [TestMethod]
        public void ConsultarTest()
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.
            Create("http://localhost:50310/ConsultasCSs.svc/Consultas/10002");
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string consultaJson = reader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            List<ConsultaCS> consLista = js.Deserialize<List<ConsultaCS>>(consultaJson);
            Assert.IsTrue(consLista.Count == 9);

        }

        [TestMethod]
        public void ConsultarTestError()
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.
                Create("http://localhost:50310/ConsultasCSs.svc/Consultas/4444");
                req.Method = "GET";
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                string consultaJson = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                List<ConsultaCS> consLista = js.Deserialize<List<ConsultaCS>>(consultaJson);
                Assert.IsTrue(consLista.Count == 9);
            }
            catch (WebException e)            {

                HttpWebResponse resError = (HttpWebResponse)e.Response;
                StreamReader reader2 = new StreamReader(resError.GetResponseStream());
                string error = reader2.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                //string errorMessage = js.Deserialize<string>(error);
                Excepcion errorMessage = js.Deserialize<Excepcion>(error);
                Assert.AreEqual("El Código de Producto 4444 no existe.", errorMessage.Mensaje);

            }
        }

        [TestMethod]
        public void ConsultarTestError2()
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.
                Create("http://localhost:50310/ConsultasCSs.svc/Consultas/10169");
                req.Method = "GET";
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                string consultaJson = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                List<ConsultaCS> consLista = js.Deserialize<List<ConsultaCS>>(consultaJson);
                Assert.IsTrue(consLista.Count == 9);
            }
            catch (WebException e)
            {

                HttpWebResponse resError = (HttpWebResponse)e.Response;
                StreamReader reader2 = new StreamReader(resError.GetResponseStream());
                string error = reader2.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                //string errorMessage = js.Deserialize<string>(error);
                Excepcion errorMessage = js.Deserialize<Excepcion>(error);
                Assert.AreEqual("Producto 10169 no enlazado a ningún centro.", errorMessage.Mensaje);
            }
        }


    }
}
